﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using VPLink2013.Framework.Extensions;
using VPLink2014.Data.MesApplis;

namespace VPLink2014
{
    public partial class ApplicationOperationsByUser : ApplicationOperations
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string responseJson = "{}";
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    if (!string.IsNullOrEmpty(this.Request.QueryString["search"]))
                    {
                        if (!string.IsNullOrEmpty(this.Request.QueryString["loginName"]))
                        {
                            string name = this.Request.QueryString["loginName"];

                            VOUserMapper user = new VOUserMapper(name);
                            List<VOApplication> apps = user.GetUserApplications();
                            if (apps.Count == 0)
                            {
                                if (!string.IsNullOrEmpty(this.Request.QueryString["filterType"])
                                && !string.IsNullOrEmpty(this.Request.QueryString["filterValue"]))
                                {
                                    string filterType = this.Request.QueryString["filterType"];
                                    string filterValue = this.Request.QueryString["filterValue"];
                                    VOTeamMapper team = _teamFactory.CreateTeam(filterType, filterValue);
                                    if (team != null)
                                    {
                                        responseJson = team.GetApplicationsTeam().ToJson();
                                    }
                                    else
                                    {
                                        //responseJson = _teamFactory.GetAllOrphanApplications().ToJson();
                                    }
                                }
                            }
                            else
                            {
                                responseJson = apps.ToJson();
                            }
                        }
                    }
                    else
                    {
                        int nbAppli = this.Request.QueryString.AllKeys.Length;
                        if (nbAppli > 1)
                        {
                            if (!string.IsNullOrEmpty(this.Request.QueryString["loginName"]))
                            {
                                string userName = this.Request.QueryString["loginName"];
                                responseJson = SetApplicationsToTeam(0, "user", userName);
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(this.Request.QueryString["keyApplication[0][value]"]))
                                {
                                    string userName = this.Request.QueryString["keyApplication[0][value]"];
                                    responseJson = SetApplicationsToTeam(nbAppli / 4, "user", userName);
                                }
                            }
                        }
                    }
                });
                Response.Write(responseJson);
                Response.End();
            }
            catch (System.Threading.ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
        }
    }
}
